﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    public class FindDocumentTreeViewItem : TreeViewItem
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullPath"></param>
        /// <param name="shortFileName">未存盘的文件需要短名才能辨别。</param>
        public FindDocumentTreeViewItem(string fullPath, string shortFileName, string customHeaderText = null)
        {
            if (string.IsNullOrEmpty(fullPath))
            {
                this.fullPath = shortFileName;
            }
            else
            {
                this.fullPath = fullPath;
            }
            this.Header = new TextBlock();
            if (string.IsNullOrWhiteSpace(customHeaderText) == false)
            {
                (this.Header as TextBlock).Text = customHeaderText;
            }
            else
            {
                (this.Header as TextBlock).Text = this.ShortName;
            }

            this.ToolTip = this.fullPath;

            this.MouseDown += FindDocumentTreeViewItem_MouseDown;
        }

        private void FindDocumentTreeViewItem_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.IsSelected = true;
        }

        private string fullPath;

        public string FullPath
        {
            get { return fullPath; }
            set
            {
                fullPath = value;
            }
        }

        public string ShortName
        {
            get
            {
                if (System.IO.File.Exists(this.fullPath) == false)
                {
                    if (string.IsNullOrEmpty(this.fullPath))
                    {
                        return "文件不存在或未存盘！";
                    }
                    else return this.fullPath;//这是考虑到未存盘的文件。
                }

                var fileInfo = new System.IO.FileInfo(this.fullPath);
                return fileInfo.Name;
            }
        }
    }
}
